<template>
  <el-form :model="form" ref="authForm" :rules="rules">
    <el-form-item label="用户名" prop="username">
      <el-input v-model.trim="form.username"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model.trim="form.password" show-password></el-input>
    </el-form-item>
    <el-form-item
      label="密码确认"
      v-if="formType == 'register'"
      prop="password_confirm"
    >
      <el-input v-model.trim="form.password_confirm" show-password></el-input>
    </el-form-item>
    <el-form-item style="text-align:center;margin: 0 2em;">
      <el-button type="primary" @click="onSubmit" size="medium" >提交</el-button>
      <el-button @click="resetForm" size="medium">清空</el-button>
      <div v-if="formType == 'login'" style="text-align:right;margin: 0 2em;"><router-link :to="{name:'ResetPassword'}">重置密码</router-link></div>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  props: {
    form: {
      type: Object,
      required: true,
    },
    formType: {
      type: String,
      default: 'register',
    },
    onSubmitReq: {
      type: Function,
      required: true,
    },
    rules: {
      type: Object,
    },
  },
  methods: {
    resetForm() {
      this.$refs.authForm.resetFields();
    },
    onSubmit() {
      this.$refs.authForm.validate((valid) => {
        if (valid) {
          this.onSubmitReq();
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style>
</style>
